/**
  ******************************************************************************
  * @file           : gui_task.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "dclcd.h"

TaskHandle_t xGui_Thd = NULL;
StaticTask_t xGui_Tcb;
StackType_t  xGui_Stk[GUI_STACK_SIZE];

const Gui_CmdQueStru gui_cmd_go_calzeroend = 
{
  .cmd = GUI_CMD_CHANGE_FORM,
  .key = IRKEY_KEY_VALUE_NULL,
  .msg = 0,
  .toform = &gui_from_calzerorunend,
};

const Gui_CmdQueStru gui_cmd_go_calgmend = 
{
  .cmd = GUI_CMD_CHANGE_FORM,
  .key = IRKEY_KEY_VALUE_NULL,
  .msg = 0,
  .toform = &gui_from_calgmrunend,
};

const Gui_CmdQueStru gui_cmd_go_calswend = 
{
  .cmd = GUI_CMD_CHANGE_FORM,
  .key = IRKEY_KEY_VALUE_NULL,
  .msg = 0,
  .toform = &gui_from_calswrunend,
};

void vGui_Task(void * pvParameters)
{
  Gui_CmdQueStru cmd;

  gui_form_exec(&gui_from_main, 0, 0);

	while(1)
	{
    if(xQueueReceive(gui_cmd_qhd, &cmd, *gui_form_current->refresh) == pdTRUE)
    {
//      dc_cmd_buzzer(10);
      if(cmd.cmd == GUI_CMD_KEY)
      {
        switch(cmd.key)
        {
          case IRKEY_KEY_VALUE_MUTE:   /* mute */
            if(gui_form_current->keys->mute != NULL)
            gui_form_current->keys->mute();
            break;
          
          case IRKEY_KEY_VALUE_POWER:   /* power */
            if(gui_form_current->keys->power != NULL)
            gui_form_current->keys->power();
            break;
          
          case IRKEY_KEY_VALUE_T:   /* keyt */
            if(gui_form_current->keys->keyt != NULL)
            gui_form_current->keys->keyt();
            break;
          
          case IRKEY_KEY_VALUE_SET:   /* set */
            if(gui_form_current->keys->set != NULL)
            gui_form_current->keys->set();
            break;
          
          case IRKEY_KEY_VALUE_HOME:   /* home */
            if(gui_form_current->keys->home != NULL)
            gui_form_current->keys->home();
            break;
          
          case IRKEY_KEY_VALUE_MENU:   /* menu */
            if(gui_form_current->keys->menu != NULL)
            gui_form_current->keys->menu();
            break;
          
          case IRKEY_KEY_VALUE_RETURN:   /* keyret */
            if(gui_form_current->keys->keyret != NULL)
            gui_form_current->keys->keyret();
            break;
          
          case IRKEY_KEY_VALUE_ENTER:   /* enter */
            if(gui_form_current->keys->enter != NULL)
            gui_form_current->keys->enter();
            break;

          case IRKEY_KEY_VALUE_DOWN:   /* down */
            if(gui_form_current->keys->down != NULL)
            gui_form_current->keys->down();
            break;

          case IRKEY_KEY_VALUE_UP:   /* up */
            if(gui_form_current->keys->up != NULL)
            gui_form_current->keys->up();
            break;
          
          case IRKEY_KEY_VALUE_LEFT:   /* left */
            if(gui_form_current->keys->left != NULL)
            gui_form_current->keys->left();
            break;
          
          case IRKEY_KEY_VALUE_RIGHT:   /* right */
            if(gui_form_current->keys->right != NULL)
            gui_form_current->keys->right();
            break;

          case IRKEY_KEY_VALUE_OK:   /* ok */
            if(gui_form_current->keys->ok != NULL)
            gui_form_current->keys->ok();
            break;

          case IRKEY_KEY_VALUE_VOLINC:   /* volinc */
            if(gui_form_current->keys->volinc != NULL)
            gui_form_current->keys->volinc();
            break;

          case IRKEY_KEY_VALUE_VOLDEC:   /* voldec */
            if(gui_form_current->keys->voldec != NULL)
            gui_form_current->keys->voldec();
            break;

          case IRKEY_KEY_VALUE_0:   /* 0 */
            if(gui_form_current->keys->key0 != NULL)
            gui_form_current->keys->key0();
            break;

          case IRKEY_KEY_VALUE_1:   /* 1 */
            if(gui_form_current->keys->key1 != NULL)
            gui_form_current->keys->key1();
            break;

          case IRKEY_KEY_VALUE_2:   /* 2 */
            if(gui_form_current->keys->key2 != NULL)
            gui_form_current->keys->key2();
            break;

          case IRKEY_KEY_VALUE_3:   /* 3 */
            if(gui_form_current->keys->key3 != NULL)
            gui_form_current->keys->key3();
            break;

          case IRKEY_KEY_VALUE_4:   /* 4 */
            if(gui_form_current->keys->key4 != NULL)
            gui_form_current->keys->key4();
            break;

          case IRKEY_KEY_VALUE_5:   /* 5 */
            if(gui_form_current->keys->key5 != NULL)
            gui_form_current->keys->key5();
            break;

          case IRKEY_KEY_VALUE_6:   /* 6 */
            if(gui_form_current->keys->key6 != NULL)
            gui_form_current->keys->key6();
            break;

          case IRKEY_KEY_VALUE_7:   /* 7 */
            if(gui_form_current->keys->key7 != NULL)
            gui_form_current->keys->key7();
            break;

          case IRKEY_KEY_VALUE_8:   /* 8 */
            if(gui_form_current->keys->key8 != NULL)
            gui_form_current->keys->key8();
            break;

          case IRKEY_KEY_VALUE_9:   /* 9 */
            if(gui_form_current->keys->key9 != NULL)
            gui_form_current->keys->key9();
            break;
            
          default:
            cmd.key = IRKEY_KEY_VALUE_FAULT;
            break;
        } /* switch(keyvalue) */
      } /* cmd.cmd == GUI_CMD_KEY */
      else if(cmd.cmd == GUI_CMD_CHANGE_FORM)
      {
        gui_form_exec(cmd.toform, cmd.msg, 0);
      }
      else
      {
        cmd.key = IRKEY_KEY_VALUE_NULL;
      }
    } 
    else /* que */
    {
      cmd.key = IRKEY_KEY_VALUE_NULL;
    }
    
    if(gui_form_current->update != NULL)
    {
      gui_form_current->update(cmd.key);
    }
	}
}
